// 对第三方插件再次进行封装使用promise
import JsonP from 'jsonp'
import axios from 'axios'
import {Modal} from 'antd'
import Utils from './../utils/utils'
export default class Axios{
    static requestList(_this, url, params,isMock) {
        var data = {
            params,
            isMock
        }
        
        this.ajax({
            url,
            data
        }).then((data) => {
            
            if (data && data.result) {
                let list;
                if(!data.result.item_list){
                    
                     list = data.result.list.map((item, index) => {
                        item.key = index;
                        return item;
                    });
                }else{
                     list = data.result.item_list.map((item, index) => {
                        item.key = index;
                        return item;
                    });
                }   
                _this.setState({
                    list: list,
                    pagination: Utils.pagination(data, (current) => {
                        _this.params.page = current;
                        _this.requestList();
                        _this.requestList();
                    })
                })
            }
        })
    }
    //static 静态方法
    static jsonp(options){
       return new Promise((resolve,reject)=>{
            JsonP(options.url,{
                param:'callback'
            },function(err,res){
               //todo-some
            //    debugger;
               if(res.status === 'success'){
                   resolve (res);
               }else{
                   reject (res.message);
               }
                
            }
            )
        })
    }
    static ajax(options){
        let loading;
        if (options.data && options.data.isShowLoading !== false) {
            loading = document.getElementById('ajaxLoading');
            loading.style.display = "block";
        }
        let baseApi = ''
        if(options.isMock){
            //mock
             baseApi ='http://106.12.220.186:4000/api'
        }else{
            // 服务器端真真实api
            baseApi = 'http://106.12.220.186:4000/api'
        }
        
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url: options.url,
                timeout: 5000,
                baseURL:baseApi,
                params: (options.data && options.data.params) || ''
            }).then((response) => {
                //    让loading消失 不显示
                if (options.data && options.data.isShowLoading !== false) {
                    loading = document.getElementById('ajaxLoading');
                    loading.style.display = "none";
                }
                if (response.status === 200) {
                    let res = response.data;
                    if (res.code === 0 ||res.code==="0") {
                        resolve(res);
                    } else {
                        Modal.info({
                            title: "提示",
                            content: res.message
                        })
                    }
                } else {
                    reject(response.data);
                }
            })
        })
    }
}